<template>
  <div class="app-container p-rel">
    <div class="container-title">查询</div>
    <form-edit class="p-abs" :detailForm="detailForm" :isEdit="isEdit" @submitForm="submitForm" :editVisible.sync="editVisible" v-if="editVisible"></form-edit>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-row :gutter="15">
          <el-form-item label="文书类型" prop="documentType">
            <el-select v-model="queryParams.documentType" placeholder="请选择类型" clearable style="width: 100%;">
              <el-option
                  v-for="dict in dict.type.document_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="文书编号" prop="documentNumber">
            <el-input
                v-model="queryParams.documentNumber"
                placeholder="请输入文书编号"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-button type="primary" icon="" size="" @click="handleQuery">搜索</el-button>
          <el-button icon="" size="" @click="resetQuery">重置</el-button>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8 tableBtnListCss">
      <el-col :span="1.5">
        <el-button
            type="primary"
            @click="handleAdd"
        >上传执法文书</el-button>

      </el-col>
    </el-row>
    <el-table v-loading="loading" border header-cell-class-name="bg-F4F8FF" max-height="580" :data="enforcementDocumentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文件类型" align="center" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.document_type" :value="scope.row.documentType"/>
        </template>
      </el-table-column>
      <el-table-column label="文书编号" align="center" prop="documentNumber" />
<!--      <el-table-column label="文件url" align="center" prop="documentFileUrl" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row,1)"

          >修改</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"

          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />
    <form-edit class="p-abs"
               :detailForm="detailForm"
               :isEdit="isEdit"
               :documentTypeList="documentTypeList"
               @submitForm="submitForm"
               :editVisible.sync="editVisible" v-if="editVisible"></form-edit>
  </div>

</template>

<script>
import { listEnforcementDocument, getEnforcementDocument, delEnforcementDocument, addEnforcementDocument, updateEnforcementDocument,getDocumentTypeList } from "@/api/dataIntegration/enforcementDocument";
import formEdit from './editFrom.vue'
export default {
  name: "EnforcementDocument",
  components:{ formEdit },
  dicts: ['document_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 执法文书表格数据
      enforcementDocumentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        documentType: null,
        documentNumber: null,
        documentFileUrl: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      detailForm:{},
      isEdit:false,
      editVisible:false,
      documentTypeList:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询执法文书列表 */
    getList() {
      this.loading = true;
      listEnforcementDocument(this.queryParams).then(response => {
        this.enforcementDocumentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        documentType: null,
        documentNumber: null,
        documentFileUrl: null,
        createId: null,
        createBy: null,
        createTime: null,
        updateId: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.detailForm = {}
      this.handleDocumentTypeList()
      this.isEdit = false
      this.editVisible = true
    },
    /** 修改按钮操作 */
    handleUpdate(row,type) {
      // this.reset();
      const id = row.id || this.ids
      getEnforcementDocument(id).then(response => {
        this.detailForm = response.data
        this.handleDocumentTypeList(row.id)
        if(type == 1){
          this.isEdit = true
        }else{ // 查看
          this.isEdit = false
        }
        this.editVisible = true


      });
    },
    handleDocumentTypeList(id){
      getDocumentTypeList({id:id}).then(response => {
        this.documentTypeList = response.data
      })

    },
    /** 提交按钮 */
    submitForm(form,id) {
      if (id != null) {
        updateEnforcementDocument(form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.editVisible = false;
          this.getList();
        });
      } else {
        addEnforcementDocument(form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.editVisible = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var ids ;
      if (row.id) {
        ids = row.id;
      } else {
        ids = this.ids
      }
      this.$modal.confirm('是否确认删除执法文书编号为"' + ids + '"的数据项？').then(function() {
        return delEnforcementDocument(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dataIntegration/enforcementDocument/export', {
        ...this.queryParams
      }, `enforcementDocument_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.app-container{
  .tableBtnListCss{
    margin-top: 10px;
    border-top: 1px solid #e0e0e0;
    padding-top: 10px;
  }
}
</style>
